package com.atguigu.gmall.mq.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.mq.config.DeadLetterMqConfig;
import com.atguigu.gmall.mq.config.DelayedMqConfig;
import com.atguigu.gmall.mq.service.RabbitService;
import net.bytebuddy.asm.Advice;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * 测试发消息
 */
@RestController
public class TestMessageSendController {


    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private RabbitService rabbitService;
    @Autowired
    private RabbitTemplate rabbitTemplate;

    //发消息
    @GetMapping("/sendMessage")
    public Result sendMessage(){
        rabbitService.sendMessage("exchange11","routingKey111","haha");
        return Result.ok();
    }

    //发消息  基于死信的延迟消息
    @GetMapping("sendDeadLetterMessage")
    public Result sendDeadLetterMessage(){

        rabbitTemplate.convertAndSend(DeadLetterMqConfig.exchange_dead,
                DeadLetterMqConfig.routing_dead_1,"111111",(message) -> {
                    //设置存活时间 （局部的）   毫秒    8s
                    message.getMessageProperties().setExpiration("8000");
                    System.out.println("发送延迟消息的时间：" + new Date());
                    return message;
                });

        return Result.ok();
    }
    //基于插件的延迟消息的发送测试
    @GetMapping("sendDelayMessage")
    public Result sendDelayMessage(){
//        rabbitTemplate.convertAndSend(DelayedMqConfig.exchange_delay,
////                DelayedMqConfig.routing_delay,"1321313131313131",(message) -> {
////                    //延迟多久   毫秒
////                    message.getMessageProperties().setDelay(7000);
////                    System.out.println("发送延迟消息的时间：" + new Date());
////                    return message;
////                });
        rabbitService.sendDelayedMessage(DelayedMqConfig.exchange_delay,DelayedMqConfig.routing_delay
        ,"1321313131313131",10000);
        return Result.ok();
    }
}
